/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.dao.DaoDisciplina;
import model.dao.DaoProfessor;
import model.dao.DaoTurma;
import model.dominio.Aluno;
import model.dominio.Disciplina;
import model.dominio.Professor;
import model.dominio.Turma;

/**
 *
 * @author Teao
 */
public class GuiTurmaCadastrar extends javax.swing.JPanel {

    private List<Aluno> listaAluno;
    private List<Professor> listaProfessor;
    private Professor professor;
    private List<Disciplina> listaDisciplina;
    private Disciplina disciplina;
    
    /**
     * Creates new form GuiProfCadastrar
     */
    public GuiTurmaCadastrar() {
        initComponents();
        DaoProfessor prof1 = new DaoProfessor();
        listaProfessor = prof1.buscarProfessor(new Professor());
        if (listaProfessor!= null){
            for (Professor professor : listaProfessor) {
            comboProfessor.addItem(professor.getNome());
            }
        }
        
        DaoDisciplina disc = new DaoDisciplina();
        listaDisciplina = disc.buscarDisciplina(new Disciplina());
        if (listaDisciplina!= null){
           for (Disciplina disciplina : listaDisciplina) {
            comboDisciplina.addItem(disciplina.getNome());
            } 
        }
        
            
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        local = new javax.swing.JTextField();
        BSalvar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabAluno = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        ano = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        periodo = new javax.swing.JTextField();
        vagas = new javax.swing.JTextField();
        horario = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        comboProfessor = new javax.swing.JComboBox();
        comboDisciplina = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setLayout(null);
        add(local);
        local.setBounds(60, 20, 190, 30);

        BSalvar.setText("Salvar");
        BSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BSalvarActionPerformed(evt);
            }
        });
        add(BSalvar);
        BSalvar.setBounds(210, 340, 160, 50);

        jLabel3.setText("Alunos:");
        add(jLabel3);
        jLabel3.setBounds(10, 220, 60, 30);

        tabAluno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}
            },
            new String [] {
                "Cpf", "Nome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabAluno);

        add(jScrollPane1);
        jScrollPane1.setBounds(60, 210, 430, 120);

        jLabel2.setText("Vagas:");
        add(jLabel2);
        jLabel2.setBounds(180, 60, 50, 30);

        jLabel4.setText("Ano:");
        add(jLabel4);
        jLabel4.setBounds(390, 20, 40, 30);

        jLabel5.setText("Local:");
        add(jLabel5);
        jLabel5.setBounds(20, 20, 50, 30);

        jLabel7.setText("Horário:");
        add(jLabel7);
        jLabel7.setBounds(10, 60, 70, 30);
        add(ano);
        ano.setBounds(430, 20, 60, 30);

        jLabel8.setText("Período:");
        add(jLabel8);
        jLabel8.setBounds(260, 20, 60, 30);
        add(periodo);
        periodo.setBounds(320, 20, 60, 30);
        add(vagas);
        vagas.setBounds(230, 60, 60, 30);
        add(horario);
        horario.setBounds(60, 60, 110, 30);

        jLabel6.setText("Prof:");
        add(jLabel6);
        jLabel6.setBounds(10, 100, 60, 30);

        comboProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboProfessorActionPerformed(evt);
            }
        });
        add(comboProfessor);
        comboProfessor.setBounds(60, 100, 430, 30);

        add(comboDisciplina);
        comboDisciplina.setBounds(60, 150, 430, 30);

        jLabel1.setText("Disc:");
        add(jLabel1);
        jLabel1.setBounds(10, 160, 49, 17);

        jButton1.setText("Remover");
        add(jButton1);
        jButton1.setBounds(500, 290, 73, 29);

        jButton2.setText("Add");
        add(jButton2);
        jButton2.setBounds(500, 220, 39, 29);
    }// </editor-fold>//GEN-END:initComponents

    private void BSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BSalvarActionPerformed
        // TODO add your handling code here:
        Turma turma = new Turma();
        if (local.getText().isEmpty() || 
            ano.getText().isEmpty() ||
            periodo.getText().isEmpty() ||
            horario.getText().isEmpty() ||
            vagas.getText().isEmpty())  {
                JOptionPane.showMessageDialog(this,"Digite os dados corretamente","ERRO", JOptionPane.ERROR_MESSAGE);
        }else{
            turma.setLocal(local.getText());
            turma.setAno(Integer.parseInt(ano.getText()));
            turma.setPeriodo(Integer.parseInt(periodo.getText()));
            turma.setHorario(Integer.parseInt(horario.getText()));
            turma.setNumeroVagas(Integer.parseInt(vagas.getText()));
            turma.setProfessor(professor);
            turma.setAlunos(/*Recebe um array de alunos selecionados da tabela*/null);
            DaoTurma dao = new DaoTurma();
            turma = (Turma) dao.salvar(turma);
            if(turma != null){
                JOptionPane.showMessageDialog(this,"Dados registrados com sucesso","Sucesso", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }
        }
    }//GEN-LAST:event_BSalvarActionPerformed

    private void CProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CProfessorActionPerformed
        
//        while (RsC1.next())  
//        {    
//            nome = RsC1.getString("Nome_CD");  
//            combo.addItem(nome);  
//        }   
//        RsC1.close();
//        {};
    }//GEN-LAST:event_CProfessorActionPerformed

    private void comboProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboProfessorActionPerformed
        // TODO add your handling code here:
        

    }//GEN-LAST:event_comboProfessorActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BSalvar;
    private javax.swing.JTextField ano;
    private javax.swing.JComboBox comboDisciplina;
    private javax.swing.JComboBox comboProfessor;
    private javax.swing.JTextField horario;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField local;
    private javax.swing.JTextField periodo;
    private javax.swing.JTable tabAluno;
    private javax.swing.JTextField vagas;
    // End of variables declaration//GEN-END:variables

    
    
    
    public void preencheTabelaAlunos(Aluno aluno) {
        List<Aluno> alunos = new ArrayList<>() ;
        DefaultTableModel model = (DefaultTableModel) tabAluno.getModel();
        String[] linha = new String[1];
        for (Aluno t : alunos) {
            linha[1] = t.getNome();
            model.addRow(linha);          
        }
    }


 }
